<?php

namespace backend\widgets;

use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use backend\assets\GridViewAsset;

class GridView extends \yii\grid\GridView {

    public $tableClass = 'zgd-grid';
    public $summary;
    public $emptyText;
    public $formatter;
    public $rowOptions;
    public $oddClass = 'odd';
    public $evenClass = 'even';
    public $pager;

    public function init() {
        $this->tableOptions = ArrayHelper::merge($this->tableOptions, ['class' => $this->tableClass]);

        $this->summary = $this->summary ? $this->summary : "<div class='summary'>第{page}/{pageCount}页，显示 {begin}-{end} 条，共 {totalCount} 条.</div>";
        $this->emptyText = $this->emptyText ? $this->emptyText : '没有数据';
        $this->formatter = $this->formatter ? $this->formatter : [
            'class' => \yii\i18n\Formatter::className(),
            'nullDisplay' => '&nbsp;'
        ];
        $this->rowOptions = $this->rowOptions ? $this->rowOptions : function ($model, $key, $index, $grid) {
            return ['class' => $index % 2 == 0 ? $this->oddClass : $this->evenClass];
        };
        $this->pager = $this->pager ? $this->pager : [
            'firstPageLabel' => '第一页',
            'lastPageLabel' => '最后一页',
            'prevPageLabel' => '上一页',
            'nextPageLabel' => '下一页',
        ];
        parent::init();
    }

    public function run() {
        $id = $this->options['id'];
        $options = Json::encode(['selectCheckedRows' => ['multiple' => true, 'tableClass' => $this->tableClass]]);
        $view = $this->getView();
        GridViewAsset::register($view);
        $view->registerJs("jQuery('#$id').zgdGridView($options);");
        parent::run();
    }

}
